﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for SectionManagement.xaml
    /// </summary>
    public partial class SectionManagement : UserControl
    {
        string id = "0";

        public SectionManagement()
        {
            InitializeComponent();
            bindGrid();
            LoadSectionIncharge();
        }

        private void LoadSectionIncharge()
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.getPermanentEmployeeList();
            cmbSecInCharge.DataContext = dt.DefaultView;
            cmbSecInCharge.DisplayMemberPath = dt.Columns["EmployeeName"].ToString();
            cmbSecInCharge.SelectedValuePath = dt.Columns["Id"].ToString();
        }

        private void clearAllFields()
        {
            txtSectionName.Text = "";
            txtDesc.Text = "";
            cmbSecInCharge.SelectedIndex = -1;
            id = "0";
        }

        private void bindGrid()
        {
            DataTable dtSections = new DataTable();
            dtSections = AbhiruchiDAL.getSections();
            gvSection.ItemsSource = dtSections;
        }

        private bool DoValidations()
        {
            if (txtSectionName.Text == "")
            {
                MessageBox.Show("Please Enter Section Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            else if (cmbSecInCharge.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Section Incharge", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.CompareSectionName(txtSectionName.Text.ToString());
            if (!(dt.Rows[0][0].ToString() == "0"))
            {
                txtSectionName.Text = "";
                MessageBox.Show("Please Give Diffrent Section Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                txtSectionName.Focus();
                return false;
            }
            return true;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            clearAllFields();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (DoValidations())
            {
                if (id == "0")
                {
                    AbhiruchiDAL.setSectionInformation(txtSectionName.Text, txtDesc.Text, Convert.ToInt32(cmbSecInCharge.SelectedValue));
                    clearAllFields();
                    MessageBox.Show("Section Information Saved Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    AbhiruchiDAL.updateSectionInformation(Convert.ToInt32(id), txtSectionName.Text, txtDesc.Text, Convert.ToInt32(cmbSecInCharge.SelectedValue));
                    clearAllFields();
                    MessageBox.Show("Section Information Modified Successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    id = "0";
                }
                bindGrid();
            }
        }

        private void gvSection_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            txtSectionName.Text=((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtDesc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            cmbSecInCharge.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString();
        }

        //private void txtSectionName_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    DataTable dt = new DataTable();
        //    dt = AbhiruchiDAL.CompareSectionName(txtSectionName.Text.ToString());
        //    if (!(dt.Rows[0][0].ToString() == "0"))
        //    {
        //        txtSectionName.Text = "";
        //        MessageBox.Show("Please Give Diffrent Section Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        //        txtSectionName.Focus();
        //    }
            

        //}
    }
}
